package com.lion.common.spring.openapi;

import com.chris.dev.base.libs.v1.http.params.NoneParam;
import com.chris.dev.base.libs.v1.http.protocols.HttpHeader;
import com.chris.dev.base.libs.v1.http.protocols.HttpRequest;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;

/**
 * @author Chris Chan
 * Create On 2025/8/1 下午5:31
 * Use for:
 * Explain:
 */
public class SchemaHelper {
    /**
     * 添加通用的Schema
     *
     * @param components OpenAPI组件
     */
    public static void addCommonSchemas(Components components) {

    }

    /**
     * 添加HttpHeader的Schema
     *
     * @param components OpenAPI组件
     */
    public static void addHttpHeaderSchema(Components components) {
        if (!components.getSchemas().containsKey("HttpHeader")) {
            components.addSchemas("HttpHeader", new Schema<>()
                    .name("HttpHeader")
                    .type("object")
                    .addProperty("ver", new StringSchema().description("版本").example("1.0.0"))
                    .addProperty("os", new StringSchema().description("平台").example("Android"))
                    .addProperty("channel", new StringSchema().description("渠道").example("HUAWEI"))
                    .addProperty("appId", new StringSchema().description("客户端ID").example("5RGJ5LIT5ASN54AK54YR"))
                    .addProperty("appSecret", new StringSchema().description("客户端密钥").example("5ASP546L55UW5ZYW6JMO"))
                    .addProperty("token", new StringSchema().description("token").example("5A6D5AGU6ZWH5RKZ5AAW"))
                    .addProperty("lng", new StringSchema().description("经度").example("107041452"))
                    .addProperty("lat", new StringSchema().description("纬度").example("33030752")));
        }
    }

    /**
     * 添加HttpRequest的Schema
     *
     * @param components OpenAPI组件
     */
    public static void addHttpRequestSchema(Components components) {
        if (!components.getSchemas().containsKey("HttpRequest")) {
            components.addSchemas("HttpRequest", new Schema<HttpRequest<?>>()
                    .name("HttpRequest")
                    .addProperty("header", new Schema<HttpHeader>().$ref("#/components/schemas/HttpHeader"))
                    .addProperty("param", new Schema<Object>().type("object")));
        }
    }

    /**
     * 添加NoneParam的Schema
     *
     * @param components OpenAPI组件
     */
    public static void addNoneParamSchema(Components components) {
        if (!components.getSchemas().containsKey("NoneParam")) {
            components.addSchemas("NoneParam", new Schema<NoneParam>()
                    .name("NoneParam"));
        }
    }

    /**
     * 添加HttpRequestNoneParam的Schema
     *
     * @param components OpenAPI组件
     */
    public static void addHttpRequestNoneParamSchema(Components components) {
        if (!components.getSchemas().containsKey("HttpRequestNoneParam")) {
            components.addSchemas("HttpRequestNoneParam", new Schema<HttpRequest<NoneParam>>()
                    .name("HttpRequestNoneParam")
                    .addProperty("header", new Schema<HttpHeader>().$ref("#/components/schemas/HttpHeader"))
                    .addProperty("param", new Schema<NoneParam>().$ref("#/components/schemas/NoneParam")));
        }
    }

    /**
     * 添加HttpResult的Schema
     *
     * @param components OpenAPI组件
     */
    public static void addHttpResultSchema(Components components) {
        Schema<?> httpResultSchema = new Schema<>()
                .name("HttpResult")
                .type("object")
                .addProperty("code", new IntegerSchema().description("状态码 0 成功,其他 失败").example(0))
                .addProperty("message", new StringSchema().description("消息").example("success"))
                .addProperty("data", new Schema<>().description("数据").type("object"));
        components.addSchemas("HttpResult", httpResultSchema);
    }

    /**
     * 添加ValueResult的Schema
     *
     * @param components OpenAPI组件
     */
    public static void addValueResultSchema(Components components) {
        if (components.getSchemas() == null ||
                !components.getSchemas().containsKey("ValueResult")) {

            Schema<?> valueResultSchema = createValueResultSchema(); // 通用object

            components.addSchemas("ValueResult", valueResultSchema);
        }
    }

    /**
     * 创建ValueResult的Schema
     *
     * @return ValueResult的Schema
     */
    public static Schema<?> createValueResultSchema() {
        return createValueResultExtSchema("ValueResult", "object");
    }

    /**
     * 创建ValueResult的Schema
     *
     * @param name      ValueResult的名称
     * @param valueType 值的类型
     * @return ValueResult的Schema
     */
    public static Schema<?> createValueResultExtSchema(String name, String valueType) {
        return new Schema<>()
                .name(name)
                .type("object")
                .addProperty("value",
                        new Schema<>()
                                .description("值 泛型数据不能以示例数据为必然结果")
                                .type(valueType));
    }
}
